package com.clstu.graph;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 研究图,图的遍历等等..
 */
public class Graph {
    private ArrayList<String> vertexList;//图的顶点集合
    private int[][] edges;//邻阶矩阵
    private int numOfEdges;//边的个数
    private boolean[] isVisited;//每个节点是否已经访问过
    public static void main(String[] args) {
        //测试
        int n = 5;//节点个数
        String[] vertexes={"A","B","C","D","E"};
        Graph graph = new Graph(n);
        for (String vertex : vertexes) {
            graph.vertexList.add(vertex);
        }
        //添加边ab ac bc bd be
        graph.addEdge(0,1,1);
        graph.addEdge(0,2,1);
        graph.addEdge(1,2,1);
        graph.addEdge(1,3,1);
        graph.addEdge(1,4,1);
        graph.showGraph();


    }

    //构造器
    public Graph(int n){
        //初始化矩阵和vertexList
        //临界矩阵
        edges = new int[n][n];
        //顶点集合
        vertexList = new ArrayList<>(n);
        //边
        numOfEdges = 0;
        //是否访问过
        isVisited = new boolean[n];
    }

    //插入顶点
    public void insertVertex(String vertex){
        vertexList.add(vertex);
    }

    //编写深度优先遍历算法
    public void dfs(){
        //思路分析,
    }
    //添加边,v1,v2表示顶点的下标,weight表示边的距离
    public void addEdge(int v1,int v2,int weight){
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;
    }

    //编写图的常用方法
    //返回顶点的个数
    public int getNumOfVertexes(){
        return vertexList.size();
    }

    //显示图的邻接矩阵
    public void showGraph(){
        for (int[] edge : edges) {
            System.out.println(Arrays.toString(edge));
        }
    }

    //得到边的数目
    public int getNumOfEdges(){
        return numOfEdges;
    }

    //返回节点对应的数据
    public String getValueByIndex(int i){
        return vertexList.get(i);
    }

    //返回两个节点直接的权值
    public int getWeight(int v1,int v2){
        return edges[v1][v2];
    }
}
